<!DOCTYPE html>
<html>
<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>传智健康</title>
    <meta name="description" content="传智健康">
    <meta name="keywords" content="传智健康">
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <!-- 引入样式 -->
    <link rel="stylesheet" href="../plugins/elementui/index.css">
    <link rel="stylesheet" href="../plugins/font-awesome/css/font-awesome.min.css">
    <link rel="stylesheet" href="../css/style.css">
    <!-- 引入组件库 -->
    <script src="../js/vue.js"></script>
    <script src="../plugins/elementui/index.js"></script>
    <script type="text/javascript" src="../js/jquery.min.js"></script>
    <script src="../js/axios-0.18.0.js"></script>
    <style type="text/css">
        .el-scrollbar__wrap {
            overflow-x: hidden;
        }
    </style>
</head>
<body>
    <div id="app">
        <!--页眉-->
        <div class="content-header">
            <h1>系统设置<small>角色管理</small></h1>
            <el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb">
                <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
                <el-breadcrumb-item>系统设置</el-breadcrumb-item>
                <el-breadcrumb-item>角色管理</el-breadcrumb-item>
            </el-breadcrumb>
        </div>

        <div class="app-container">
            <!--上下界限（上蓝下黑）-->
            <div class="box">
                <div class="filter-container">
                    <el-input placeholder="角色名称/关键字" v-model="pagination.queryString" style="width: 200px;" class="filter-item"></el-input>
                    <el-button @click="findPage()" class="dalfBut">查询</el-button>
                    <el-button type="primary" class="butT" @click="handleCreate()">新建</el-button>
                </div>
            </div>
        </div>

        <!-- 数据表格 -->
        <el-table size="small" current-row-key="id" :data="dataList" stripe highlight-current-row>
            <el-table-column type="index" align="center" label="序号"></el-table-column>
            <el-table-column prop="name" label="角色名称" align="center"></el-table-column>
            <el-table-column prop="keyword" label="角色关键字" align="center"></el-table-column>
            <el-table-column prop="description" label="描述" align="center"></el-table-column>
            <el-table-column label="操作" align="center">
                <template slot-scope="scope">
                    <el-button type="primary" size="mini" @click="handleUpdate(scope.row)">编辑</el-button>
                    <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>

        <!-- 新增角色弹层 -->
        <div class="add-form">
            <el-dialog title="新增角色" :visible.sync="dialogFormVisible4Add">
                <el-form ref="dataAddForm" :model="formData" :rules="rules" label-position="right" label-width="100px">
                    <el-row>
                        <el-col :span="24">
                            <el-form-item label="角色名称" prop="name">
                                <el-input v-model="formData.name" @blur="checkRoleName(formData.name)"/>
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <el-row>
                        <el-col :span="24">
                            <el-form-item label="角色关键字" prop="keyword">
                                <el-input v-model="formData.keyword" @blur="checkRoleKeyWord(formData.keyword)"/>
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <el-row>
                        <el-col :span="24">
                            <el-form-item label="描述">
                                <el-input v-model="formData.description"/>
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <el-row>

                        <!--菜单复选框列表-->
                        <el-col :span="12" align="middle">
                            菜单选择
                            <el-scrollbar style="height:200px">
                                <!--树形控件-->
                                <el-tree
                                        :data="tableData4menus"
                                        show-checkbox
                                        node-key="id"
                                        ref="tree"
                                        :props="props">
                                </el-tree>
                            </el-scrollbar>
                        </el-col>
                        <!--权限复选框列表-->
                        <el-col :span="12">
                            <div align="middle">权限选择</div>
                            <el-scrollbar style="height:200px">
                            <el-form-item>
                                <table class="datatable">
                                    <tbody>
                                    <tr v-for="c in tableData4permissions">
                                        <td>
                                            <input :id="c.id" v-model="permissions" type="checkbox" :value="c.id">
                                        </td>
                                        <td><label :for="c.id">{{c.name}}</label></td>
                                    </tr>
                                    </tbody>
                                </table>
                            </el-form-item>
                            </el-scrollbar>
                        </el-col>
                    </el-row>
                </el-form>

                <div slot="footer" class="dialog-footer">
                    <el-button @click="dialogFormVisible4Add = false">取消</el-button>
                    <el-button type="primary" @click="handleAdd()">确定</el-button>
                </div>
            </el-dialog>
        </div>

        <!-- 编辑角色弹层 -->
        <div class="add-form">
            <el-dialog title="编辑角色" :visible.sync="dialogFormVisible4Edit">
                <el-form ref="dataEditForm" :model="formData" :rules="rules" label-position="right" label-width="100px">
                    <el-row>
                        <el-col :span="24">
                            <el-form-item label="角色名称" prop="name">
                                <el-input v-model="formData.name"/>
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <el-row>
                        <el-col :span="24">
                            <el-form-item label="角色关键字" prop="keyword">
                                <el-input v-model="formData.keyword"/>
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <el-row>
                        <el-col :span="24">
                            <el-form-item label="描述">
                                <el-input v-model="formData.description"/>
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <el-row>

                        <!--菜单复选框列表-->
                        <el-col :span="12" align="middle">
                            菜单选择
                            <el-scrollbar style="height:200px">
                                <!--树形控件-->
                                <el-tree
                                        :data="tableData4menus"
                                        show-checkbox
                                        node-key="id"
                                        ref="tree"
                                        :default-checked-keys="defaultKeys"
                                        :props="props"
                                ></el-tree>
                            </el-scrollbar>
                        </el-col>
                        <!--权限复选框列表-->
                        <el-col :span="12">
                            <div align="middle">权限选择</div>
                            <el-scrollbar style="height:200px">
                                <el-form-item>
                                    <table class="datatable">
                                        <tbody>
                                        <tr v-for="c in tableData4permissions">
                                            <td>
                                                <input :id="c.id" v-model="permissions" type="checkbox" :value="c.id">
                                            </td>
                                            <td><label :for="c.id">{{c.name}}</label></td>
                                        </tr>
                                        </tbody>
                                    </table>
                                </el-form-item>
                            </el-scrollbar>
                        </el-col>
                    </el-row>
                </el-form>

                <div slot="footer" class="dialog-footer">
                    <el-button @click="dialogFormVisible4Edit = false">取消</el-button>
                    <el-button type="primary" @click="handleEdit()">确定</el-button>
                </div>
            </el-dialog>
        </div>

        <!--分页控件-->
        <div class="pagination-container">
            <el-pagination
                    class="pagiantion"
                    @current-change="handleCurrentChange"
                    :current-page="pagination.currentPage"
                    :page-size="pagination.pageSize"
                    layout="total, prev, pager, next, jumper"
                    :total="pagination.total">
            </el-pagination>
        </div>



    </div>
</body>

</html>
<script>
    var checkRoleName_flag;
    //自定义验证
    var checkRoleName = (rule, value, callback) => {
        setTimeout(() => {
            if (!checkRoleName_flag) {
                callback();
            }else {
                callback(new Error('角色名称重复'));
            }
        }, 1000);
    }
    var checkRoleKeyWord_flag;
    var checkRoleKeyWord = (rule, value, callback) => {
        setTimeout(() => {
            if (!checkRoleKeyWord_flag) {
                callback();
            }else {
                callback(new Error('角色关键字重复'));
            }
        }, 1000);
    }
</script>

<script>
    var vue = new Vue({
        el: '#app',
        data: {
            pagination: {//分页相关模型数据
                currentPage: 1,//当前页码
                pageSize:10,//每页显示的记录数
                total:0,//总记录数
                queryString:null//查询条件
            },

            defaultKeys:[],//树形控件中默认选中的keys
            //props:渲染规则
            props: {
                multiple: true,//设置为多选
                id:"id",
                label: "name",//label值和哪个值绑定
                children: "children"//children值和哪个值绑定
            },
            dataList: [],//当前页要展示的分页列表数据
            formData: {},//表单数据

            tableData4menus:[], //添加表单窗口中菜单列表数据
            tableData4permissions:[], //添加表单窗口中权限列表数据

            menus:[],//添加和编辑表单窗口中菜单复选框对应id
            permissions:[],//添加和编辑表单窗口中权限复选框对应id

            dialogFormVisible4Add:false,//添加表单是否可见
            dialogFormVisible4Edit:false,//编辑表单是否可见

            rules: {//校验规则
                name: [{ required: true, message: '角色名称为必填项', trigger: 'blur' },
                    { validator: checkRoleName, trigger: 'blur' }],
                keyword: [{ required: true, message: '角色关键字为必填项', trigger: 'blur' },
                    { validator: checkRoleKeyWord, trigger: 'blur' }]
            },


        },
        methods:{
            //分页查询
            findPage(){
                var parms = {
                    currentPage:this.pagination.currentPage,
                    pageSize:this.pagination.pageSize,
                    queryString:this.pagination.queryString
                }
                axios.post("/role/findPage.do",parms).then(res=>{
                    //后台返回的是PageResult total rows
                    this.pagination.total = res.data.total;
                    this.dataList = res.data.rows;
                })
            },
            //页面跳转
            handleCurrentChange(currentPage){
                this.pagination.currentPage = currentPage;
                this.findPage();
            },
            // 重置表单
            resetForm() {
                this.formData = {};//清空
                this.permissions = [];
            },
            //新建弹窗
            handleCreate(){
                //重置表单
                this.resetForm();
                //显示浮层
                this.dialogFormVisible4Add = true;
                //获取菜单列表
                axios.get("/menu/findAll.do").then(response=>{
                    //反馈响应信息
                    this.$message({
                        message:response.data.message,
                        type:response.data.flag? 'success':'error'
                    })
                    //绑定数据
                    this.tableData4menus = response.data.data;
                })

                //获取权限列表
                axios.get("/permission/findAll.do").then(response=>{
                    //反馈响应信息
                    this.$message({
                        message:response.data.message,
                        type:response.data.flag? 'success':'error'
                    })
                    //绑定数据
                    this.tableData4permissions = response.data.data;
                })
            },
            //新建
            handleAdd(){
                //绑定已选菜单列表id
                this.getCheckedKeys();
                //提交前校验
                this.$refs["dataAddForm"].validate((valid) => {
                    if (valid) {
                        axios.post("/role/add.do?permissions=" + this.permissions + "&menus=" + this.menus, this.formData).then(response => {
                            this.$message({
                                //执行添加
                                message: response.data.message,
                                type: response.data.flag ? 'success' : 'error'
                            });
                            //刷新页面
                            this.findPage();
                        });
                        //隐藏浮层
                        this.dialogFormVisible4Add = false;
                    }
                });

            },
            //编辑弹窗
            handleUpdate(row){
                //显示浮层
                this.dialogFormVisible4Edit = true;
                //回显角色数据
                axios.get("/role/findById.do?roleId="+row.id).then(response=>{
                    this.$message({
                        message: response.data.message,
                        type: response.data.flag ? 'success' : 'error'
                    });
                    if (response.data.flag){
                        //绑定角色表单数据
                        this.formData = response.data.data;

                        //回显菜单列表
                        axios.get("/menu/findAll.do").then(response=>{
                            this.$message({
                                message:response.data.message,
                                type:response.data.flag? 'success':'error'
                            });
                            if (response.data.flag){
                                //绑定菜单列表
                                this.tableData4menus = response.data.data;
                                //回显角色可访问的菜单列表
                                axios.get("/role/findMenusByRoleId.do?roleId="+row.id).then(response=> {
                                    //绑定菜单复选框数据
                                    this.defaultKeys = response.data;
                                });
                            };
                        });

                        //回显权限列表
                        axios.get("/permission/findAll.do").then(response=>{
                            this.$message({
                                message:response.data.message,
                                type:response.data.flag? 'success':'error'
                            });
                            if (response.data.flag){
                                //绑定权限列表
                                this.tableData4permissions = response.data.data;
                                //回显角色可访问的权限列表
                                axios.get("/role/findPermissionsByRoleId.do?roleId="+row.id).then(response=> {
                                    //绑定权限复选框数据
                                    this.permissions = response.data;
                                });
                            };
                        });
                    }
                })
            },
            //编辑
            handleEdit(){
                //绑定已选菜单列表id
                this.getCheckedKeys();
                //提交前校验
                this.$refs["dataEditForm"].validate((valid) => {
                    if (valid) {
                        //执行编辑
                        axios.post("/role/edit.do?permissions=" + this.permissions + "&menus=" + this.menus, this.formData).then(response => {
                            this.$message({
                                message: response.data.message,
                                type: response.data.flag ? 'success' : 'error'
                            });
                            //刷新页面
                            this.findPage()
                        });
                        //隐藏浮层
                        this.dialogFormVisible4Edit = false;
                    }
                })

            },
            //删除
            handleDelete(row){
                //1、弹窗确认
                this.$confirm("此操作将永久删除该数据, 是否继续?","提示",{type:'warning'}).then(()=>{
                    //2、发送ajax请求
                    axios.get("/role/delete.do?roleId="+row.id).then((response)=>{
                        this.$message({
                            message:response.data.message,
                            type:response.data.flag? 'success':'error'
                        });
                        // 刷新页面
                        this.findPage();
                    })
                });
            },
            //获取菜单id列表
            getCheckedKeys() {
                //getCheckedKeys()获取全选框ids,getHalfCheckedKeys()获取半选框
                this.menus = [];
                this.menus = this.menus.concat(this.$refs.tree.getCheckedKeys(),this.$refs.tree.getHalfCheckedKeys());
                console.log(this.menus);
            },
            //角色名查重
            checkRoleName(name){
                axios.get("/role/checkRoleName.do?roleName="+name).then((response)=>{
                    checkRoleName_flag = response.data.flag;
                })
            },
            //角色关键字查重
            checkRoleKeyWord(keyWord){
                axios.get("/role/checkRoleKeyWord.do?roleKeyWord="+keyWord).then((response)=>{
                    checkRoleKeyWord_flag = response.data.flag;
                })
            }
        },
        //钩子函数，VUE对象初始化完成后自动执行
        created() {
            //查询所有检查项列表
            this.findPage();
        },
    })
</script>

